/*
 * Copyright (c) 田梓萱 2021-2022.
 * All Rights Reserved.
 * All codes are protected by China's regulations on the protection of computer software, and infringement must be investigated.
 * 版权所有 (c) 田梓萱 2021-2022.
 * 所有代码均受中国《计算机软件保护条例》保护，本项目已获得软著.
 */

package com.xcl.calculator;

import com.huawei.hms.analytics.HiAnalyticsInstance;
import com.huawei.hms.analytics.HiAnalyticsTools;
import com.litesuits.orm.LiteOrm;
import com.net.jianjia.JianJia;
import com.net.jianjia.gson.GsonConverterFactory;
import com.xcl.calculator.DataANet.Showmes;
import com.xcl.calculator.DataANet.Wan;
import ohos.aafwk.ability.AbilityPackage;
import ohos.app.Context;
import ohos.data.distributed.common.KvManagerConfig;
import ohos.data.distributed.common.KvManagerFactory;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

import java.util.UUID;
//TODO:实时数据上报关键字：HiAnalyticsSDK

/**
 * The type My application.
 *
 * @author Xcl
 * @date 2021 /12/31
 * @package com.xcl.calculator
 */
public class MyApplication extends AbilityPackage {
    /**
     * The constant label.
     */
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00234, "MyApplication");
    /**
     * The constant liteOrm.
     */
    public static LiteOrm liteOrm;
    /**
     * The constant instance_hi.
     */
    public static HiAnalyticsInstance instance_hi;
    /**
     * The constant Instance.
     */
    private static MyApplication Instance;
    /**
     * The Devicetype.
     */
    private int Devicetype = 999666;
    /**
     * The M jian jia.
     */
    private JianJia mJianJia;
    /**
     * The M wan.
     */
    private Wan mWan;

    /**
     * Gets instance.
     *
     * @return the instance
     */
    public static MyApplication getInstance() {
        return Instance;
    }

    /**
     * 获取全局的蒹葭对象
     *
     * @return 全局的蒹葭对象 jian jia
     */
    public JianJia getJianJia() {
        return this.mJianJia;
    }

    /**
     * 获取全局的接口实例对象
     *
     * @return 全局的接口实例对象 wan
     */
    public Wan getWan() {
        return this.mWan;
    }

    /**
     * 初始化
     */
    @Override
    public void onInitialize() {
        super.onInitialize();
        try {
            Context context1 = this.getContext();
            Preference_RW.context = context1;
            XLog.pd_pd = Preference_RW.ff7_r();
            if (liteOrm == null) liteOrm = LiteOrm.newSingleInstance(this, "bianqianshow.db");
            //TODO:测试专用开始，正式版务必注释
            //liteOrm.setDebugged(true);
            //HiAnalyticsTools.enableLog();
            //this.mJianJia = new JianJia.Builder()
            // 使用自定义的okHttpClient对象
            //        .baseUrl("https://api.xuegao-xcl.tech")
            //        .addConverterFactory(GsonConverterFactory.create())
            //        .build();
            //TODO:测试专用结束
            if (Preference_RW.ff7_r() == 555) {
                //TODO:Debug开发者模式
                liteOrm.setDebugged(Preference_RW.ff7_r() == 555); // open the log
                //TODO:打开SDK日志开关
                HiAnalyticsTools.enableLog();
                //TODO:创建全局的蒹葭对象且全日志
                //创建日志拦截器
                HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
                logging.setLevel(HttpLoggingInterceptor.Level.BODY);
                //为OKHTTP添加日志拦截器
                OkHttpClient okHttpClient = new OkHttpClient.Builder()
                        .addInterceptor(logging)
                        .build();
                this.mJianJia = new JianJia.Builder()
                        // 使用自定义的okHttpClient对象
                        .callFactory(okHttpClient)
                        .baseUrl("https://api.xxxxx.xxx")//TODO:汇率接口地址
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            } else {
                liteOrm.setDebugged(false);
                //TODO:创建全局的蒹葭对象
                this.mJianJia = new JianJia.Builder()
                        // 使用自定义的okHttpClient对象
                        .baseUrl("https://api.xxxxx.xxx")//TODO:汇率接口地址
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
            Preference_RW.uuid = UUID.randomUUID();
            ohos.data.distributed.device.DeviceInfo localDeviceInfo = KvManagerFactory.getInstance()
                    .createKvManager(new KvManagerConfig((this))).getLocalDeviceInfo();
            int localDeviceType = Integer.parseInt(localDeviceInfo.getType());
            this.Devicetype = localDeviceType;
            Preference_RW.setLocalDeviceType(this.Devicetype);
            Showmes sh2 = new Showmes();
            if (Preference_RW.ff1_r() == 0) {
                long a1 = Preference_RW.ff3_r();
                a1++;
                Preference_RW.ff3_w((a1 + 1));
                long a2 = Preference_RW.ff4_r();
                a2++;
                Preference_RW.ff4_w((a2 + 1));
                sh2.setId(1);
                sh2.setTitle("使用说明");
                final String chushimes = ("\n" + "1.进入主菜单后点击对应功能即可跳转" + "\n" +
                        "2.每一个功能中可以通过左右滑动来切换，且每个模块右滑均可以回到菜单" + "\n" +
                        "3.多功能科学计算上下滑动查看输入所有功能函数" + "\n" +
                        "4.多功能计算器有误差，如需追求高精度，请选择高精度大数计算" + "\n" +
                        "5.汇率计算需要联网获取当天的汇率，仅供参考" + "\n" +
                        "6.汇率计算快慢与您的网络状况有关，点计算后请耐心等待" + "\n" +
                        "7.高精度大数计算仅支持连续计算" + "\n" +
                        "8.进制转换最大支持36进制，大于10进制的数请通过系统自带键盘中输入" + "\n" +
                        "9.多功能科学计算长按每个按钮可以查看每个按钮使用说明" + "\n" +
                        "10.进制转换需要点击右侧按钮后再依次输入数据后，方可计算" + "\n" +
                        "11.时间戳计算会自动根据位数来选择秒/毫秒为基准来求解" + "\n" +
                        "12.时间差计算需要仅时、分、秒支持适配表冠" + "\n" +
                        "13.分布式便签中标题请务必简短，过长标题需要在小窗中滑动查看" + "\n" +
                        "14.分布式便签建议2个设备同时使用，本功能依赖于鸿蒙分布式能力，便签数据不仅支持快速分布式同步，亦支持保存在本地" + "\n" +
                        "15.分布式便签第一页本地信息保存，不会自动销毁，第二页仅用于同步，离开即销毁" + "\n" +
                        "16.所有输入输出框中的数据显示不全时，请左右或上下滑动来查看" + "\n" +
                        "17.关于界面可以联系我、反馈，也可以查看我的最新隐私协议" + "\n" +
                        "18.这个是便签中预留数据，本信息可以删除");
                sh2.setMessage(chushimes);
                liteOrm.save(sh2);
            }
            Instance = this;
            this.mWan = this.mJianJia.create(Wan.class);
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }
}